package com.haven123.cloudapp.service.a.controller;

import com.haven123.cloudapp.service.a.util.SpringContextUtils;
import groovy.lang.GroovyClassLoader;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.lang.reflect.Method;

@Slf4j
@RestController
public class GroovyController   {
    @RequestMapping("/runGroovy")
    public String runGroovy(String script) throws Exception {
        log.info(script);
        GroovyClassLoader groovyClassLoader = new GroovyClassLoader();
        Class aClass = groovyClassLoader.parseClass(script);
        Object o = aClass.newInstance();
        SpringContextUtils.autowireBean(o);
        Method run1 = aClass.getMethod("run");
        Object run = run1.invoke(o);
        return String.valueOf(run);
    }

}
